
from typing import List
from models.account import Account
from models.user import User
from database.database import Database

class UserTable(Database):

    def __init__(self):
        self.table_name = 'user'

    def insert(self, info : User):
        sql = self.get_insert_sql_from_obj(info, self.table_name)
        self.execute(sql)
        return int(self.cursor.lastrowid)

    def getUser(self, userid : int) -> User :
        sql = f'select * from {self.table_name} where `id` = %s'
        self.execute(sql,(userid))
        result = self.cursor.fetchone()
        if not result:
            return None
        return User(result)

    def login(self, login : Account) -> int :
        sql = f'select id from {self.table_name} where `username` = %s and `password` = %s'
        self.execute(sql,(login.username,login.password))
        result = self.cursor.fetchone()
        if not result:
            return 0
        return result['id']

